import sys
import threading
import time
import cv2
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog



    

class Ui_Detect(object):
    def __init__(self):
        self.isCapture = False

    def setupUi(self, Detect):
        Detect.setObjectName("Detect")
        Detect.resize(762, 633)
        self.gridLayout = QtWidgets.QGridLayout(Detect)
        self.gridLayout.setObjectName("gridLayout")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.lineEdit = QtWidgets.QLineEdit(Detect)
        self.lineEdit.setInputMask("")
        self.lineEdit.setText("")
        self.lineEdit.setObjectName("lineEdit")
        self.horizontalLayout.addWidget(self.lineEdit)
        self.pushButton = QtWidgets.QPushButton(Detect)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(Detect)
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout.addWidget(self.pushButton_2)
        self.pushButton_3 = QtWidgets.QPushButton(Detect)
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout.addWidget(self.pushButton_3)
        self.pushButton_4 = QtWidgets.QPushButton(Detect)
        self.pushButton_4.setObjectName("pushButton_4")
        self.horizontalLayout.addWidget(self.pushButton_4)
        self.pushButton_5 = QtWidgets.QPushButton(Detect)
        self.pushButton_5.setObjectName("pushButton_5")
        self.horizontalLayout.addWidget(self.pushButton_5)
        self.horizontalLayout.setStretch(0, 1)
        self.horizontalLayout.setStretch(1, 1)
        self.horizontalLayout.setStretch(2, 1)
        self.horizontalLayout.setStretch(3, 1)
        self.horizontalLayout.setStretch(4, 1)
        self.horizontalLayout.setStretch(5, 1)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.label = QtWidgets.QLabel(Detect)
        self.label.setText("")
        self.label.setObjectName("label")
        self.verticalLayout.addWidget(self.label)
        self.verticalLayout.setStretch(0, 1)
        self.verticalLayout.setStretch(1, 8)
        self.gridLayout.addLayout(self.verticalLayout, 0, 0, 1, 1)

        self.retranslateUi(Detect)
        QtCore.QMetaObject.connectSlotsByName(Detect)

    def retranslateUi(self, Detect):
        _translate = QtCore.QCoreApplication.translate
        Detect.setWindowTitle(_translate("Detect", "Form"))
        self.pushButton.setText(_translate("Detect", "确定"))
        self.pushButton_2.setText(_translate("Detect", "打开图片"))
        self.pushButton_3.setText(_translate("Detect", "打开摄像头"))
        self.pushButton_4.setText(_translate("Detect", "截图"))
        self.pushButton_5.setText(_translate("Detect", "检测"))
        self.pushButton_3.clicked.connect(self.open)
        self.pushButton_4.clicked.connect(self.capture)

    def open(self):
        print("打开摄像头")
        cap = cv2.VideoCapture(0)
        while cap.isOpened():
            ret, frame = cap.read()
            img = QtGui.QImage(frame.data, frame.shape[1], frame.shape[0], QtGui.QImage.Format_BGR888)
            self.label.setPixmap(QtGui.QPixmap.fromImage(img))
            self.label.setScaledContents(True)
            cv2.waitKey(0)
    """
        这一种方式不弹窗，直接输入路径保存截图
    """
    # def capture(self):
    #     self.label.pixmap().save("./image/1234.jpg")
    #     print("保存完毕")

    """
        这种方式会弹出窗口，自定义目录以及文件名
    """
    def capture(self):
        filename, _ = QFileDialog.getSaveFileName(None, 'Save Image', '.', 'Image files (*.jpg *.png)')
        if not filename:
            return
        try:
            self.label.pixmap().save(filename)
            print("截图保存成功！")
        except Exception as e:
            print("截图保存失败！", str(e))


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QWidget()
    ui = Ui_Detect()  # 一定要分开写
    ui.setupUi(window)
    # ui.wait_update()
    window.show()
    sys.exit(app.exec_())
